package com.pos.app.util.http;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HttpContext;

import android.content.Context;
import android.os.Message;

import com.pos.app.CustomMessage;
import com.pos.app.exception.CustomExceptionMessage;

/**
 * @Description MySyncHttpClient是网络请求的一个类,可以支持异步,也可以不开启异步来处理程序
 */
public class MySyncHttpClient extends AsyncHttpClient
{
	private int responseCode;
	protected String result;
	private CustomMessage cm = new CustomMessage();

	protected AsyncHttpResponseHandler responseHandler = new AsyncHttpResponseHandler()
	{
		@Override
		protected void sendResponseMessage(HttpResponse response)
		{
			super.sendResponseMessage(response);

			responseCode = response.getStatusLine().getStatusCode();
			if (responseCode == 401)
			{
				cm.setCode(2);
			}
		}

		@Override
		protected void sendMessage(Message msg)
		{
			handleMessage(msg);
		}

		@Override
		public void onSuccess(String content)
		{
			if (content.indexOf("code") != -1)
			{
				cm.setCode(3);//
				cm.setMessage("请求服务器500，请与管理员联系！");
			}
			else
			{
				cm.setCode(1);
				cm.setJsonStr(content);
			}
		}

		@Override
		public void onFailure(Throwable error, String content)
		{
			cm.setCode(-1);
			cm.setMessage(CustomExceptionMessage.getMessageByException(error));// 获取错误信息
		}
	};

	public int getResponseCode()
	{
		return responseCode;
	}

	public CustomMessage getCustomMessage()
	{
		return cm;
	}

	@Override
	protected void sendRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest, String contentType, AsyncHttpResponseHandler responseHandler, Context context)
	{
		uriRequest.addHeader("X-Requested-With", "XMLHttpRequest");// 后台验证
		if (contentType != null)
		{
			uriRequest.addHeader("Content-Type", contentType);
		}
		new AsyncHttpRequest(client, httpContext, uriRequest, responseHandler).run();
	}

	public String onRequestFailed(Throwable error, String content)
	{
		return "";
	}

	public void delete(String url, RequestParams queryParams, AsyncHttpResponseHandler responseHandler)
	{
		delete(url, responseHandler);
	}

	public String get(String url, RequestParams params)
	{
		this.get(url, params, responseHandler);
		return result;
	}

	public String get(String url)
	{
		this.get(url, null, responseHandler);
		return result;
	}

	public String put(String url, RequestParams params)
	{
		this.put(url, params, responseHandler);
		return result;
	}

	public String put(String url)
	{
		this.put(url, null, responseHandler);
		return result;
	}

	public String post(String url, RequestParams params)
	{
		this.post(url, params, responseHandler);
		return result;
	}

	public String post(String url)
	{
		this.post(url, null, responseHandler);
		return result;
	}

	public String delete(String url, RequestParams params)
	{
		this.delete(url, params, responseHandler);
		return result;
	}

	public String delete(String url)
	{
		this.delete(url, null, responseHandler);
		return result;
	}

	// 传输JSON格式
	public String post(String url, String json)
	{
		try
		{
			//TODO:url = Urls.URLNew + url;
			StringEntity se = new StringEntity(json, "UTF-8");
			
			this.post(null, url, null, se, "application/json", responseHandler);
		}
		catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * post上传文件
	 * @param url
	 * @param imageUrl
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String postImage(String url, String imageUrl) throws UnsupportedEncodingException
	{
		if (!imageUrl.equals(""))
		{
			File file = new File(imageUrl);
			//FileBody bin = new FileBody(file);
			MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset.forName("UTF-8"));
			reqEntity.addPart("file", new FileBody(file));
			//TODO:url = Urls.URLNew + url;
			this.post(null, url, null, reqEntity, null, responseHandler);
		}
		return result;
	}
}
